home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '87 / Source ƒ.sea / Source ƒ / emacs source ƒ / WINDOW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-28  |  15.8 KB  |  574 lines  |  [TEXT/MARC]

  1. /*
  2.  * Window management. Some of the functions are internal, and some are
  3.  * attached to keys that the user actually types.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include        "estruct.h"
  8. #include    "edef.h"
  9.  
  10. /*
  11.  * Reposition dot in the current window to line "n". If the argument is
  12.  * positive, it is that line. If it is negative it is that line from the
  13.  * bottom. If it is 0 the window is centered (this is what the standard
  14.  * redisplay code does). With no argument it defaults to 0. Bound to M-!.
  15.  */
  16. reposition(f, n)
  17.     {
  18.     if (f == FALSE)    /* default to 0 to center screen */
  19.     n = 0;
  20.     curwp->w_force = n;
  21.     curwp->w_flag |= WFFORCE;
  22.     return (TRUE);
  23.     }
  24.  
  25. /*
  26.  * Refresh the screen. With no argument, it just does the refresh. With an
  27.  * argument it recenters "." in the current window. Bound to "C-L".
  28.  */
  29. refresh(f, n)
  30.     {
  31.     if (f == FALSE)
  32.         sgarbf = TRUE;
  33.     else
  34.         {
  35.         curwp->w_force = 0;             /* Center dot. */
  36.         curwp->w_flag |= WFFORCE;
  37.         }
  38.  
  39.     return (TRUE);
  40.     }
  41.  
  42. /*
  43.  * The command make the next window (next => down the screen) the current
  44.  * window. There are no real errors, although the command does nothing if
  45.  * there is only 1 window on the screen. Bound to "C-X C-N".
  46.  *
  47.  * with an argument this command finds the <n>th window from the top
  48.  *
  49.  */
  50. nextwind(f, n)
  51.  
  52. int f, n;    /* default flag and numeric argument */
  53.  
  54. {
  55.     register WINDOW *wp;
  56.     register int nwindows;        /* total number of windows */
  57.  
  58.     if (f) {
  59.  
  60.         /* first count the # of windows */
  61.         wp = wheadp;
  62.         nwindows = 1;
  63.         while (wp->w_wndp != NULL) {
  64.             nwindows++;
  65.             wp = wp->w_wndp;
  66.         }
  67.  
  68.         /* if the argument is negative, it is the nth window
  69.            from the bottom of the screen            */
  70.         if (n < 0)
  71.             n = nwindows + n + 1;
  72.  
  73.         /* if an argument, give them that window from the top */
  74.         if (n > 0 && n <= nwindows) {
  75.             wp = wheadp;
  76.             while (--n)
  77.                 wp = wp->w_wndp;
  78.         } else {
  79.             mlwrite("Window number out of range");
  80.             return(FALSE);
  81.         }
  82.     } else
  83.         if ((wp = curwp->w_wndp) == NULL)
  84.             wp = wheadp;
  85.     curwp = wp;
  86.     curbp = wp->w_bufp;
  87.     upmode();
  88.     return (TRUE);
  89. }
  90.  
  91. /*
  92.  * This command makes the previous window (previous => up the screen) the
  93.  * current window. There arn't any errors, although the command does not do a
  94.  * lot if there is 1 window.
  95.  */
  96. prevwind(f, n)
  97. {
  98.     register WINDOW *wp1;
  99.     register WINDOW *wp2;
  100.     register int nwindows;    /* total # of windows */
  101.  
  102.     /* if we have an argument, we mean the nth window from the bottom */
  103.     if (f)
  104.         return(nextwind(f, -n));
  105.  
  106.     wp1 = wheadp;
  107.     wp2 = curwp;
  108.  
  109.     if (wp1 == wp2)
  110.         wp2 = NULL;
  111.  
  112.     while (wp1->w_wndp != wp2)
  113.         wp1 = wp1->w_wndp;
  114.  
  115.     curwp = wp1;
  116.     curbp = wp1->w_bufp;
  117.     upmode();
  118.     return (TRUE);
  119. }
  120.  
  121. /*
  122.  * This command moves the current window down by "arg" lines. Recompute the
  123.  * top line in the window. The move up and move down code is almost completely
  124.  * the same; most of the work has to do with reframing the window, and picking
  125.  * a new dot. We share the code by having "move down" just be an interface to
  126.  * "move up". Magic. Bound to "C-X C-N".
  127.  */
  128. mvdnwind(f, n)
  129.     int n;
  130.     {
  131.     return (mvupwind(f, -n));
  132.     }
  133.  
  134. /*
  135.  * Move the current window up by "arg" lines. Recompute the new top line of
  136.  * the window. Look to see if "." is still on the screen. If it is, you win.
  137.  * If it isn't, then move "." to center it in the new framing of the window
  138.  * (this command does not really move "."; it moves the frame). Bound to
  139.  * "C-X C-P".
  140.  */
  141. mvupwind(f, n)
  142.     int n;
  143.     {
  144.     register LINE *lp;
  145.     register int i;
  146.  
  147.     lp = curwp->w_linep;
  148.  
  149.     if (n < 0)
  150.         {
  151.         while (n++ && lp!=curbp->b_linep)
  152.             lp = lforw(lp);
  153.         }
  154.     else
  155.         {
  156.         while (n-- && lback(lp)!=curbp->b_linep)
  157.             lp = lback(lp);
  158.         }
  159.  
  160.     curwp->w_linep = lp;
  161.     curwp->w_flag |= WFHARD;            /* Mode line is OK. */
  162.  
  163.     for (i = 0; i < curwp->w_ntrows; ++i)
  164.         {
  165.         if (lp == curwp->w_dotp)
  166.             return (TRUE);
  167.         if (lp == curbp->b_linep)
  168.             break;
  169.         lp = lforw(lp);
  170.         }
  171.  
  172.     lp = curwp->w_linep;
  173.     i  = curwp->w_ntrows/2;
  174.  
  175.     while (i-- && lp != curbp->b_linep)
  176.         lp = lforw(lp);
  177.  
  178.     curwp->w_dotp  = lp;
  179.     curwp->w_doto  = 0;
  180.     return (TRUE);
  181.     }
  182.  
  183. /*
  184.  * This command makes the current window the only window on the screen. Bound
  185.  * to "C-X 1". Try to set the framing so that "." does not have to move on the
  186.  * display. Some care has to be taken to keep the values of dot and mark in
  187.  * the buffer structures right if the distruction of a window makes a buffer
  188.  * become undisplayed.
  189.  */
  190. onlywind(f, n)
  191. {
  192.         register WINDOW *wp;
  193.         register LINE   *lp;
  194.         register int    i;
  195.  
  196.         while (wheadp != curwp) {
  197.                 wp = wheadp;
  198.                 wheadp = wp->w_wndp;
  199.                 if (--wp->w_bufp->b_nwnd == 0) {
  200.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  201.                         wp->w_bufp->b_doto  = wp->w_doto;
  202.                         wp->w_bufp->b_markp = wp->w_markp;
  203.                         wp->w_bufp->b_marko = wp->w_marko;
  204.                 }
  205.                 free((char *) wp);
  206.         }
  207.         while (curwp->w_wndp != NULL) {
  208.                 wp = curwp->w_wndp;
  209.                 curwp->w_wndp = wp->w_wndp;
  210.                 if (--wp->w_bufp->b_nwnd == 0) {
  211.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  212.                         wp->w_bufp->b_doto  = wp->w_doto;
  213.                         wp->w_bufp->b_markp = wp->w_markp;
  214.                         wp->w_bufp->b_marko = wp->w_marko;
  215.                 }
  216.                 free((char *) wp);
  217.         }
  218.         lp = curwp->w_linep;
  219.         i  = curwp->w_toprow;
  220.         while (i!=0 && lback(lp)!=curbp->b_linep) {
  221.                 --i;
  222.                 lp = lback(lp);
  223.         }
  224.         curwp->w_toprow = 0;
  225.         curwp->w_ntrows = term.t_nrow-1;
  226.         curwp->w_linep  = lp;
  227.         curwp->w_flag  |= WFMODE|WFHARD;
  228.         return (TRUE);
  229. }
  230.  
  231. /*
  232.  * Delete the current window, placing its space in the window above,
  233.  * or, if it is the top window, the window below. Bound to C-X 0.
  234.  */
  235.  
  236. delwind(f,n)
  237.  
  238. int f, n;    /* arguments are ignored for this command */
  239.  
  240. {
  241.     register WINDOW *wp;    /* window to recieve deleted space */
  242.     register WINDOW *lwp;    /* ptr window before curwp */
  243.     register int target;    /* target line to search for */
  244.  
  245.     /* if there is only one window, don't delete it */
  246.     if (wheadp->w_wndp == NULL) {
  247.         mlwrite("Can not delete this window");
  248.         return(FALSE);
  249.     }
  250.  
  251.     /* find window before curwp in linked list */
  252.     wp = wheadp;
  253.     lwp = NULL;
  254.     while (wp != NULL) {
  255.         if (wp == curwp)
  256.             break;
  257.         lwp = wp;
  258.         wp = wp->w_wndp;
  259.     }
  260.  
  261.     /* find recieving window and give up our space */
  262.     wp = wheadp;
  263.     if (curwp->w_toprow == 0) {
  264.         /* find the next window down */
  265.         target = curwp->w_ntrows + 1;
  266.         while (wp != NULL) {
  267.             if (wp->w_toprow == target)
  268.                 break;
  269.             wp = wp->w_wndp;
  270.         }
  271.         if (wp == NULL)
  272.             return(FALSE);
  273.         wp->w_toprow = 0;
  274.         wp->w_ntrows += target;
  275.     } else {
  276.         /* find the next window up */
  277.         target = curwp->w_toprow - 1;
  278.         while (wp != NULL) {
  279.             if ((wp->w_toprow + wp->w_ntrows) == target)
  280.                 break;
  281.             wp = wp->w_wndp;
  282.         }
  283.         if (wp == NULL)
  284.             return(FALSE);
  285.         wp->w_ntrows += 1 + curwp->w_ntrows;
  286.     }
  287.  
  288.     /* get rid of the current window */
  289.     if (--curwp->w_bufp->b_nwnd == 0) {
  290.         curwp->w_bufp->b_dotp = curwp->w_dotp;
  291.         curwp->w_bufp->b_doto = curwp->w_doto;
  292.         curwp->w_bufp->b_markp = curwp->w_markp;
  293.         curwp->w_bufp->b_marko = curwp->w_marko;
  294.     }
  295.     if (lwp == NULL)
  296.         wheadp = curwp->w_wndp;
  297.     else
  298.         lwp->w_wndp = curwp->w_wndp;
  299.     free((char *)curwp);
  300.     curwp = wp;
  301.     wp->w_flag |= WFHARD;
  302.     curbp = wp->w_bufp;
  303.     upmode();
  304.     return(TRUE);
  305. }
  306.  
  307. /*
  308.  
  309. Split the current window.  A window smaller than 3 lines cannot be
  310. split.  An argument of 1 forces the cursor into the upper window, an
  311. argument of two forces the cursor to the lower window.  The only other
  312. error that is possible is a "malloc" failure allocating the structure
  313. for the new window.  Bound to "C-X 2". 
  314.  
  315.  */
  316. splitwind(f, n)
  317.  
  318. int f, n;    /* default flag and numeric argument */
  319.  
  320. {
  321.         register WINDOW *wp;
  322.         register LINE   *lp;
  323.         register int    ntru;
  324.         register int    ntrl;
  325.         register int    ntrd;
  326.         register WINDOW *wp1;
  327.         register WINDOW *wp2;
  328.     char *malloc();
  329.  
  330.         if (curwp->w_ntrows < 3) {
  331.                 mlwrite("Cannot split a %d line window", curwp->w_ntrows);
  332.                 return (FALSE);
  333.         }
  334.         if ((wp = (WINDOW *) malloc(sizeof(WINDOW))) == NULL) {
  335.                 mlwrite("Cannot allocate WINDOW block");
  336.                 return (FALSE);
  337.         }
  338.         ++curbp->b_nwnd;                        /* Displayed twice.     */
  339.         wp->w_bufp  = curbp;
  340.         wp->w_dotp  = curwp->w_dotp;
  341.         wp->w_doto  = curwp->w_doto;
  342.         wp->w_markp = curwp->w_markp;
  343.         wp->w_marko = curwp->w_marko;
  344.         wp->w_flag  = 0;
  345.         wp->w_force = 0;
  346. #if    COLOR
  347.     /* set the colors of the new window */
  348.     wp->w_fcolor = gfcolor;
  349.     wp->w_bcolor = gbcolor;
  350. #endif
  351.         ntru = (curwp->w_ntrows-1) / 2;         /* Upper size           */
  352.         ntrl = (curwp->w_ntrows-1) - ntru;      /* Lower size           */
  353.         lp = curwp->w_linep;
  354.         ntrd = 0;
  355.         while (lp != curwp->w_dotp) {
  356.                 ++ntrd;
  357.                 lp = lforw(lp);
  358.         }
  359.         lp = curwp->w_linep;
  360.         if (((f == FALSE) && (ntrd <= ntru)) || ((f == TRUE) && (n == 1))) {
  361.                 /* Old is upper window. */
  362.                 if (ntrd == ntru)               /* Hit mode line.       */
  363.                         lp = lforw(lp);
  364.                 curwp->w_ntrows = ntru;
  365.                 wp->w_wndp = curwp->w_wndp;
  366.                 curwp->w_wndp = wp;
  367.                 wp->w_toprow = curwp->w_toprow+ntru+1;
  368.                 wp->w_ntrows = ntrl;
  369.         } else {                                /* Old is lower window  */
  370.                 wp1 = NULL;
  371.                 wp2 = wheadp;
  372.                 while (wp2 != curwp) {
  373.                         wp1 = wp2;
  374.                         wp2 = wp2->w_wndp;
  375.                 }
  376.                 if (wp1 == NULL)
  377.                         wheadp = wp;
  378.                 else
  379.                         wp1->w_wndp = wp;
  380.                 wp->w_wndp   = curwp;
  381.                 wp->w_toprow = curwp->w_toprow;
  382.                 wp->w_ntrows = ntru;
  383.                 ++ntru;                         /* Mode line.           */
  384.                 curwp->w_toprow += ntru;
  385.                 curwp->w_ntrows  = ntrl;
  386.                 while (ntru--)
  387.                         lp = lforw(lp);
  388.         }
  389.         curwp->w_linep = lp;                    /* Adjust the top lines */
  390.         wp->w_linep = lp;                       /* if necessary.        */
  391.         curwp->w_flag |= WFMODE|WFHARD;
  392.         wp->w_flag |= WFMODE|WFHARD;
  393.         return (TRUE);
  394. }
  395.  
  396. /*
  397.  * Enlarge the current window. Find the window that loses space. Make sure it
  398.  * is big enough. If so, hack the window descriptions, and ask redisplay to do
  399.  * all the hard work. You don't just set "force reframe" because dot would
  400.  * move. Bound to "C-X Z".
  401.  */
  402. enlargewind(f, n)
  403. {
  404.         register WINDOW *adjwp;
  405.         register LINE   *lp;
  406.         register int    i;
  407.  
  408.         if (n < 0)
  409.                 return (shrinkwind(f, -n));
  410.         if (wheadp->w_wndp == NULL) {
  411.                 mlwrite("Only one window");
  412.                 return (FALSE);
  413.         }
  414.         if ((adjwp=curwp->w_wndp) == NULL) {
  415.                 adjwp = wheadp;
  416.                 while (adjwp->w_wndp != curwp)
  417.                         adjwp = adjwp->w_wndp;
  418.         }
  419.         if (adjwp->w_ntrows <= n) {
  420.                 mlwrite("Impossible change");
  421.                 return (FALSE);
  422.         }
  423.         if (curwp->w_wndp == adjwp) {           /* Shrink below.        */
  424.                 lp = adjwp->w_linep;
  425.                 for (i=0; i<n && lp!=adjwp->w_bufp->b_linep; ++i)
  426.                         lp = lforw(lp);
  427.                 adjwp->w_linep  = lp;
  428.                 adjwp->w_toprow += n;
  429.         } else {                                /* Shrink above.        */
  430.                 lp = curwp->w_linep;
  431.                 for (i=0; i<n && lback(lp)!=curbp->b_linep; ++i)
  432.                         lp = lback(lp);
  433.                 curwp->w_linep  = lp;
  434.                 curwp->w_toprow -= n;
  435.         }
  436.         curwp->w_ntrows += n;
  437.         adjwp->w_ntrows -= n;
  438.         curwp->w_flag |= WFMODE|WFHARD;
  439.         adjwp->w_flag |= WFMODE|WFHARD;
  440.         return (TRUE);
  441. }
  442.  
  443. /*
  444.  * Shrink the current window. Find the window that gains space. Hack at the
  445.  * window descriptions. Ask the redisplay to do all the hard work. Bound to
  446.  * "C-X C-Z".
  447.  */
  448. shrinkwind(f, n)
  449. {
  450.         register WINDOW *adjwp;
  451.         register LINE   *lp;
  452.         register int    i;
  453.  
  454.         if (n < 0)
  455.                 return (enlargewind(f, -n));
  456.         if (wheadp->w_wndp == NULL) {
  457.                 mlwrite("Only one window");
  458.                 return (FALSE);
  459.         }
  460.         if ((adjwp=curwp->w_wndp) == NULL) {
  461.                 adjwp = wheadp;
  462.                 while (adjwp->w_wndp != curwp)
  463.                         adjwp = adjwp->w_wndp;
  464.         }
  465.         if (curwp->w_ntrows <= n) {
  466.                 mlwrite("Impossible change");
  467.                 return (FALSE);
  468.         }
  469.         if (curwp->w_wndp == adjwp) {           /* Grow below.          */
  470.                 lp = adjwp->w_linep;
  471.                 for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_linep; ++i)
  472.                         lp = lback(lp);
  473.                 adjwp->w_linep  = lp;
  474.                 adjwp->w_toprow -= n;
  475.         } else {                                /* Grow above.          */
  476.                 lp = curwp->w_linep;
  477.                 for (i=0; i<n && lp!=curbp->b_linep; ++i)
  478.                         lp = lforw(lp);
  479.                 curwp->w_linep  = lp;
  480.                 curwp->w_toprow += n;
  481.         }
  482.         curwp->w_ntrows -= n;
  483.         adjwp->w_ntrows += n;
  484.         curwp->w_flag |= WFMODE|WFHARD;
  485.         adjwp->w_flag |= WFMODE|WFHARD;
  486.         return (TRUE);
  487. }
  488.  
  489. /*    Resize the current window to the requested size    */
  490.  
  491. resize(f, n)
  492.  
  493. int f, n;    /* default flag and numeric argument */
  494.  
  495. {
  496.     int clines;    /* current # of lines in window */
  497.     
  498.     /* must have a non-default argument, else ignore call */
  499.     if (f == FALSE)
  500.         return(TRUE);
  501.  
  502.     /* find out what to do */
  503.     clines = curwp->w_ntrows;
  504.  
  505.     /* already the right size? */
  506.     if (clines == n)
  507.         return(TRUE);
  508.  
  509.     return(enlargewind(TRUE, n - clines));
  510. }
  511.  
  512. /*
  513.  * Pick a window for a pop-up. Split the screen if there is only one window.
  514.  * Pick the uppermost window that isn't the current window. An LRU algorithm
  515.  * might be better. Return a pointer, or NULL on error.
  516.  */
  517. WINDOW  *
  518. wpopup()
  519. {
  520.         register WINDOW *wp;
  521.  
  522.         if (wheadp->w_wndp == NULL              /* Only 1 window        */
  523.         && splitwind(FALSE, 0) == FALSE)        /* and it won't split   */
  524.                 return (NULL);
  525.         wp = wheadp;                            /* Find window to use   */
  526.         while (wp!=NULL && wp==curwp)
  527.                 wp = wp->w_wndp;
  528.         return (wp);
  529. }
  530.  
  531. scrnextup(f, n)        /* scroll the next window up (back) a page */
  532.  
  533. {
  534.     nextwind(FALSE, 1);
  535.     backpage(f, n);
  536.     prevwind(FALSE, 1);
  537. }
  538.  
  539. scrnextdw(f, n)        /* scroll the next window down (forward) a page */
  540.  
  541. {
  542.     nextwind(FALSE, 1);
  543.     forwpage(f, n);
  544.     prevwind(FALSE, 1);
  545. }
  546.  
  547. savewnd(f, n)        /* save ptr to current window */
  548.  
  549. {
  550.     swindow = curwp;
  551.     return(TRUE);
  552. }
  553.  
  554. restwnd(f, n)        /* restore the saved screen */
  555.  
  556. {
  557.     register WINDOW *wp;
  558.  
  559.     /* find the window */
  560.     wp = wheadp;
  561.     while (wp != NULL) {
  562.         if (wp == swindow) {
  563.             curwp = wp;
  564.             curbp = wp->w_bufp;
  565.             upmode();
  566.             return (TRUE);
  567.         }
  568.         wp = wp->w_wndp;
  569.     }
  570.  
  571.     mlwrite("[No such window exists]");
  572.     return(FALSE);
  573. }
  574.